<?
class IndexController extends Zend_Controller_Action
{

	public function init()
	{
		
		$this->user=$userData=Zend_Auth::getInstance();
		/* Initialize action controller here */
	}

	function preDispatch()
	{
		$auth = Zend_Auth::getInstance();

		if (!$auth->hasIdentity()) {
			$this->_redirect('login/login');
		}
	}

	public function indexAction()
	{
		//$this->_redirect("/index/campaignlist");
		// action body
	}

	public function testAction(){

	}

	function campaignlistAction(){


		$this->view->title='Доступные кампании';

		$userData=Zend_Auth::getInstance()->getIdentity();
		$table = new ezCampaigns();
		$this->view->camp=$table->fetchAll(

		$res=$table->select()
		->setIntegrityCheck(false)
		->from('ez_campaigns')
		->columns("IF(access='open',1,0) as toread")		
		->joinLeft('ez_users_campaigns','ez_users_campaigns.campaign_id = ez_campaigns.id',array('user_id'=>'user_id','campstatus'=>'campstatus'))
		->where("access = 'open'")->group('id')
		)->toArray();
		
		if (is_array($this->view->camp)){
			foreach ($this->view->camp as $k=>$v){
				
			}
		}
		//echo " $res ";
		//var_dump($this->view->camp);

	}

	function mycampaignlistAction(){

		//$this->view->setScriptPath('views/scripts/index/campaignList.phtml');
		$this->view->title = "Мои кампании";
		$userData=Zend_Auth::getInstance()->getIdentity();
		$table = new ezCampaigns();
		$this->view->camp=$table->fetchAll(
		$res=$table->select()
		->setIntegrityCheck(false)
		->from('ez_campaigns')
		->join('ez_users_campaigns','ez_users_campaigns.campaign_id = ez_campaigns.id',array('user_id'=>'user_id','campstatus'=>'campstatus'))
		->where('user_id = ?',$userData->id)
		)->toArray();
		//echo " $res ";
		//var_dump($$this->view->camp);
		$this->render("campaignlist");	
	}
	
	function campaignAction(){
		
		$filter = new Zend_Filter();
		$camp= new ezCampaigns();
		$id=$filter->filter($this->_request->getParam('id'));
		
		$campRow = ($camp->fetchRow(
		
			$camp
				->select()
				->setIntegrityCheck(false)
				->from('ez_campaigns as c')
				->columns("IF(access='open',1,0) as toread , IF(access='open',1,0) as tojoin")
				->joinLeft('ez_users_campaigns as uc','uc.campaign_id=c.id AND uc.user_id='.$this->user->getIdentity()->id,"campstatus")
				->where('c.id = ?',$id)
		)->toArray());
		
		if ($campRow['campstatus']==''){
			//unset($campRow['description']);
			unset($campRow['showstart']);
		} else {
		$this->view->assign($campRow);			
		$tasks= new ezTasks();
		$st = $tasks->fetchAll(
			$tasks->select()
			->setIntegrityCheck(false)
			->where('campaign_id = ?',$id)
		)->toarray();
		
		$this->view->tasks=$st;
		}
		$this->view->assign($campRow);
		
	}
	
	function jointocompanyAction(){
		$filter = new Zend_Filter();
		$camp= new ezCampaigns();
		$id=$filter->filter($this->_request->getParam('id'));
		
		$campRow = $camp->fetchRow(
		
			$camp
				->select()
				->setIntegrityCheck(false)
				->from('ez_campaigns as c')
				->columns("IF(access='open',1,0) as toread , IF(access='open',1,0) as tojoin")
				->joinLeft('ez_users_campaigns as uc','uc.campaign_id=c.id AND uc.user_id='.$this->user->getIdentity()->id,"campstatus")
				->where('c.id = ?',$id)
		)->toArray();
		if ($campRow['tojoin']==1 and $campRow['campstatus']===null){
			$usercamp = new ezUsersCampaign();
			echo $usercamp->insert(array('user_id'=>$this->user->getIdentity()->id,"campaign_id"=>$id,"campstatus"=>'player')); 
			
		}
		//$this->render();
		//$this->_redirect($this->view->url(array("controller"=>"index",'action'=>'campaign')));
	}

}
